var cxgd = {
    setBtnStatus:function () {
        var row = $('#grid_cxgd').datagrid('getSelected');
    },
    alwayShowDetail:function (rowData) {
//        $('#layout_cxgd').layout('remove', 'center');
//        $('#layout_cxgd').layout('add', {
//            region:'center',
//            split:true,
//            href:$.webPath() + '/kcgl/cxgd/cxgd_detail?khId=' + rowData.id
//        });
    },
    showDetail:function (rowData) {
//        if (rowData && $("#cxgd_detail_id").val() != rowData.id) {
//            cxgd.alwayShowDetail(rowData);
//        }
    }
}
$(function () {

    //表格初始加载
    $('#grid_cxgd').datagrid({
        url:$.webPath() + "/kcgl/cxgd/cxgdList",
        toolbar:"#toolbar_cxgd",
        fitColumns:false,
        singleSelect:false,
        pagination:true,
        pageNumber:1,
        fit:true,
        checked:true,
        checkOnSelect:true,
        selectOnCheck:false,
        rownumbers:true,
        striped:true,
        showFooter:true,
        frozenColumns:[
            [
                {field:'checked', title:'选择', align:'center', checkbox:true},
                {field:'khbm', align:'center', title:'卡号', width:150},
                {field:'code', align:'center', title:'物资编码', width:100},
                {field:'name', align:'center', title:'物资名称'},
                {field:'ggxh', align:'center', title:'规格型号'},
                {field:'cd', align:'center', title:'产地'},
                {field:'dw', align:'center', title:'单位'}
            ]
        ],
        columns:[
            [

                {field:'measure_count', align:'right', title:'挂单件数', width:90},
                {field:'total_measure', align:'right', title:'挂单件重', width:90},
                {field:'market_price', align:'right', title:'挂牌价(元)', width:90,
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }
                },
                {field:'measure', align:'right', title:'单位件重', width:90},
                {field:'warehouse', align:'center', title:'仓库', width:100},
                {field:"user_id", align:'center', title:"订单号"},
                {field:"cggs_qc", align:'center', title:"采购公司"},
                {field:"sbgs_qc", align:'center', title:"申报公司"},
                {field:"gys_qc", align:'center', title:"供应商"},
                {field:"production_date", align:'center', title:"生成日期"}
            ]
        ],
        onLoadSuccess:function () {
            //$(this).datagrid('selectRow', 0);
            cxgd.setBtnStatus();

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function () {
            cxgd.setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //查询按钮
    $("#btn_cxgd_search").click(function () {
        cxgd_search();
    });

    //撤单按钮
    $("#cxgd_cd").click(function () {
        cxgd_cd();
    });

});

//撤单
function cxgd_cd() {
    var dataChecked = $("#grid_cxgd").datagrid("getChecked");
    if (20 >= dataChecked.length > 0) {
        $.messager.confirm('提示', '是否进行撤单？', function (r) {
            if (r) {
                var gdIds = "";
                $.each(dataChecked, function (i, val) {
                    gdIds += (val['id'] + ',');
                });
                $.ajax({
                    url:$.webPath() + "/kcgl/cxgd/cxgd_cd",
                    dataType:'json',
                    type:'POST',
                    data:{gdIds:gdIds},
                    beforeSend:function () {
                        $("#layout_cxgd").mask();
                    },
                    success:function (data) {
                        $("#layout_cxgd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_cxgd').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                height:200,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_cxgd').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else if (dataChecked.length > 20) {
        $.messager.alert("提示", "一次最多只能撤单20条记录！", "warning");
    } else {
        $.messager.alert("提示", "请选择要撤单的卡号！", "warning");
    }
}

//实时库存查询
function cxgd_search() {
    var ckmc = $('#cxgd_ckmc').val() == null ? null : $('#cxgd_ckmc').val();
    var code = $('#cxgd_code').val() == null ? null : $('#cxgd_code').val();
    var ggxh = $('#cxgd_ggxh').val() == null ? null : $('#cxgd_ggxh').val();
    var wzmc = $('#cxgd_wzmc').val() == null ? null : $('#cxgd_wzmc').val();
    var khbm = $('#cxgd_khbm').val() == null ? null : $('#cxgd_khbm').val();
    $('#grid_cxgd').datagrid('load',
        {
            ckmc:ckmc,
            code:code,
            ggxh:ggxh,
            wzmc:wzmc,
            khbm:khbm
        }
    );
}




